var pageNo=1;
var pageSize=10;
var flag=1;
$(document).ready(function(){
    showList();
});
//创建页码信息
function createPageInfo(list) {
    $("#startRow").text(list.startRow);
    $("#endRow").text(list.endRow);
    $("#totalPage").text(list.total);
    pageNum=list.pages;
    var temp = '';
    for (var i = 1; i < list.pages + 1; i++) {
        temp += '<option value="' + i + '">' + i + '</option>'
    }
    $("#pageInfo").html(temp)
        .val(list.pageNum)
        .change(function () {
            $("#pageInfor").blur();
            if (flag !== 1) {
                return
            }
            pageNo = this.options[this.options.selectedIndex].value;
            showList();
        });
    $("#firstPage").click(function () {
        if (pageNo!= 1) {
            if (flag !== 1) {
                return
            }
            pageNo = 1;
            showList();
        }
    });
    $("#lastPage").click(function () {
        if (pageNo < pageNum) {
            if (flag !== 1) {
                return
            }
            pageNo = pageNum;
            showList();
        }
    });
    $("#nextPage").click(function () {
        if (pageNo < pageNum) {
            if (flag !== 1) {
                return
            }
            pageNo++;
            showList();
        }
    });
    $("#previousPage").click(function () {
        if (pageNo > 1) {
            if (flag !== 1) {
                return
            }
            pageNo--;
            showList();
        }
    });
    $("#pageSizeInfo").change(function () {
        $("#pageSizeInfo").blur();
        if (flag !== 1) {
            return
        }
        pageSize = this.options[this.options.selectedIndex].value;
        showList();
    });
}
//取消选中
$("#cancelConnect").click(function(){
    $('.i-checks').iCheck('uncheck');
});
//关联店铺
$("#submit").click(function () {
    chooseSupplier();
});
//选择供应商
function chooseSupplier() {
    var checked=$('div.iradio_square-green.checked')[0];
    if(!checked){
        delete window.localStorage.supplierConnect;
    }else{
        var td=checked.parentNode.parentNode;
        var supplierId=$(td).attr("data-id");
        var supplierName=$(td).attr("data-name");
        var supplierConnect={"supplierId":supplierId,"supplierName":supplierName};
        setStorJson("supplierConnect",supplierConnect);
    }
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index); //再执行关闭
}
/////table的ajax请求
function showList() {
    if (flag !== 1) {
        return;
    }
    flag = 0;
    ajax({
        url:"goodsSupplier/queryList",
        data:{
            pageNo:pageNo,
            pageSize:pageSize
        },
        success:function(data){
            
            flag=1;
            if(data.success){
                createPageInfo(data.data);
                document.getElementById("tbody").innerHTML=template("template",data.data);
                $('.i-checks').iCheck({
                    checkboxClass: 'icheckbox_square-green',
                    radioClass: 'iradio_square-green'
                });
            }else{
                layer.msg(data.message);
            }
        }
    });
}
